package org.hamster.multidropbox.mainView.view.file
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	
	import mx.collections.ArrayCollection;
	import mx.collections.IList;
	import mx.controls.Text;
	import mx.controls.TextInput;
	import mx.core.IFlexDisplayObject;
	import mx.managers.PopUpManager;
	
	import org.hamster.common.components.listSelector.events.ListSelectorEvent;
	import org.hamster.common.manager.pointcut.vo.PointcutVO;
	import org.hamster.common.puremvc.constants.PointcutTiming;
	import org.hamster.common.puremvc.mediator.BaseMediator;
	import org.hamster.common.utils.ArrayUtils;
	import org.hamster.multidropbox.dropbox.event.AppDropboxEvent;
	import org.hamster.multidropbox.dropbox.vo.AppDropboxClient;
	import org.hamster.multidropbox.dropbox.vo.AppDropboxLinkFoldersVO;
	import org.hamster.multidropbox.main.constants.AppPopUpButtonConstants;
	import org.hamster.multidropbox.main.services.AppAlertPopUpService;
	import org.hamster.multidropbox.main.services.AppTipService;
	import org.hamster.multidropbox.main.utils.AppUtil;
	import org.hamster.multidropbox.main.utils.PureMVCHelper;
	import org.hamster.multidropbox.mainView.event.MainViewEvent;
	
	public class LinkFolderPanelMediator extends BaseMediator
	{
		public static const NAME:String = "org.hamster.multidropbox.mainView.view.file.LinkFolderPanelMediator";
		
		public static const S_SELECT_ACCOUNT:uint    = 0x1;
		public static const S_FOLDER_FULFILLED:uint  = 0x2;
		public static const S_FOLDER_LINKING:uint    = 0x4;
		public static const S_FOLDER_LINKED:uint     = 0x8;
		
		public function LinkFolderPanelMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
		}
		
		override public function onRegister():void
		{
			this.pointcutManager.addPointcut(AppDropboxEvent.DROPBOX_LINK_FOLDERS_REQUEST, PointcutTiming.BEFORE_SEND, null, 
				AppAlertPopUpService.instance.create(
					AppUtil.getStr("warning"), 
					AppUtil.getStrMainView('linkFolderPanel.addWarning'), 
					AppPopUpButtonConstants.OK_CANCEL, 
					[
						{
							type : AppPopUpButtonConstants.CANCEL, 
							handler: function():void {
								setStatus(S_FOLDER_FULFILLED);
							}
						}
					]));
			
			PureMVCHelper.appLoadingMaskMediator.addPair(AppDropboxEvent.DROPBOX_LINK_FOLDERS_REQUEST,
				AppDropboxEvent.DROPBOX_LINK_FOLDERS_RESULT, AppUtil.getStrMainView('linkFolderPanel.linkingFolders')); 
			PureMVCHelper.appLoadingMaskMediator.addPair(AppDropboxEvent.DROPBOX_LINK_FOLDERS_REQUEST,
				AppDropboxEvent.DROPBOX_LINK_FOLDERS_FAULT, AppUtil.getStrMainView('linkFolderPanel.linkingFolders')); 
		}
		
		override public function onRemove():void
		{
			this.pointcutManager.cleanPointcuts();
		}
		
		override protected function statusChanged():void
		{
			if (app) {
				app.linkFolderTI.enabled = isStatusAvailable(S_SELECT_ACCOUNT);
				app.addBtn.enabled       = isStatusAvailable(S_FOLDER_FULFILLED);
				app.saveBtn.enabled      = isStatusAvailable(S_FOLDER_LINKED);
			}
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				MainViewEvent.LINK_FOLDER_POPUP_SHOW,
				MainViewEvent.LINK_FOLDER_POPUP_HIDE,
				AppDropboxEvent.DROPBOX_LINK_FOLDERS_RESULT,
				AppDropboxEvent.DROPBOX_STORAGE_SAVE_RESULT,
				AppDropboxEvent.DROPBOX_STORAGE_SAVE_FAULT,
			];
		}
		
		public function handleLinkFolderPopupShow(data:Object):void
		{
			viewComponent = AppUtil.popup(LinkFolderPanel);
			
			var dropboxClientList:ArrayCollection = ArrayUtils.clone(PureMVCHelper.dropboxClientListProxy.dropboxClientList);
			app.accountAvailableList = dropboxClientList;
			
			app.accountListSelector.addEventListener(ListSelectorEvent.SELECTION_CHANGED, accountListSelectorSelectionChangedHandler);
			app.addBtn.addEventListener(MouseEvent.CLICK, addBtnClickHandler);
			app.saveBtn.addEventListener(MouseEvent.CLICK, saveBtnClickHandler);
			app.cancelBtn.addEventListener(MouseEvent.CLICK, cancelBtnClickHandler);
			app.linkFolderTI.addEventListener(Event.CHANGE, linkFolderTextChangeHandler);
			
			statusChanged();
		}
		
		public function handleLinkFolderPopupHide(data:Object):void
		{
			app.accountListSelector.removeEventListener(ListSelectorEvent.SELECTION_CHANGED, accountListSelectorSelectionChangedHandler);
			app.addBtn.removeEventListener(MouseEvent.CLICK, addBtnClickHandler);
			app.cancelBtn.removeEventListener(MouseEvent.CLICK, cancelBtnClickHandler);
			app.linkFolderTI.removeEventListener(Event.CHANGE, linkFolderTextChangeHandler);
			
			PopUpManager.removePopUp(viewComponent as IFlexDisplayObject);
			this.viewComponent = null;
			this.viewStatus = 0;
		}
		
		public function handleDropboxLinkFoldersResult(data:Object):void
		{
			this.app.resultList = new ArrayCollection((data as AppDropboxLinkFoldersVO).itemList);
			setStatus(S_FOLDER_LINKED);
		}
		
		public function handleDropboxStorageSaveResult(data:Object):void
		{
			if (viewComponent != null) {
				this.sendNotification(MainViewEvent.LINK_FOLDER_POPUP_HIDE);
			}
		}
		
		public function handleDropboxStorageSaveFault(data:Object):void
		{
			AppTipService.instance.error(AppUtil.getStrStatus("tip.saveUserFileFailed.title"), AppUtil.getStrStatus("tip.saveUserFileFailed.text"));
		}
		
		private function linkFolderTextChangeHandler(event:Event):void
		{
			this.setStatusAvailable(S_FOLDER_FULFILLED, app.linkFolderTI.text.length > 0);
		}
		
		private function accountListSelectorSelectionChangedHandler(event:ListSelectorEvent):void
		{
			var length:int = app.accountListSelector.rightListDataProvider ? app.accountListSelector.rightListDataProvider.length : 0;
			this.setStatusAvailable(S_SELECT_ACCOUNT, length > 0);
		}
		
		private function addBtnClickHandler(event:MouseEvent):void
		{
			var rightDataProvider:IList = app.accountListSelector.rightListDataProvider;
			var accountUids:Array = new Array();
			for each (var client:AppDropboxClient in rightDataProvider) {
				accountUids.push(client.uid);
			}
			
			this.setStatus(S_FOLDER_LINKING);
			
			this.sendNotification(AppDropboxEvent.DROPBOX_LINK_FOLDERS_REQUEST, 
			{
				folderName  : app.linkFolderTI.text,
				accountUids : accountUids
			});
		}
		
		private function saveBtnClickHandler(event:MouseEvent):void
		{
			this.sendNotification(AppDropboxEvent.DROPBOX_STORAGE_SAVE_REQUEST);
		}
		
		private function cancelBtnClickHandler(event:MouseEvent):void
		{
			this.sendNotification(MainViewEvent.LINK_FOLDER_POPUP_HIDE);
		}
		
		public function get app():LinkFolderPanel
		{
			return viewComponent as LinkFolderPanel;
		}
	}
}